package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.os.Bundle;
import android.view.View;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.CollectAdapter;
import flc.ast.databinding.ActivityCollectBinding;
import flc.ast.utils.AppSpUtils;
import stark.common.bean.StkResBean;

public class CollectActivity extends BaseAc<ActivityCollectBinding> {

    private CollectAdapter collectAdapter;
    private List<StkResBean> listShow = new ArrayList<>();
    private List<StkResBean> listSel = new ArrayList<>();
    private boolean isEdit = false;

    @Override
    protected int onCreate() {
        return R.layout.activity_collect;
    }

    @Override
    public void onBackPressed() {
        if (isEdit) {
            cancelEdit();
        } else {
            finish();
        }
    }

    @Override
    protected void initView() {
        mDataBinding.ivCollectBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.ivCollectEdit.setOnClickListener(this);
        mDataBinding.tvDelete.setOnClickListener(this);

        mDataBinding.rvCollectList.setLayoutManager(new LinearLayoutManager(mContext));
        collectAdapter = new CollectAdapter();
        collectAdapter.setEdit(isEdit);
        mDataBinding.rvCollectList.setAdapter(collectAdapter);
        collectAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivCollectEdit://编辑
                isEdit = true;
                collectAdapter.setEdit(isEdit);
                collectAdapter.notifyDataSetChanged();
                mDataBinding.llDelete.setVisibility(View.VISIBLE);
                break;
            case R.id.tvDelete://删除
                if (listSel.size() == 0) {
                    ToastUtils.showShort(getString(R.string.please_sel_caricature));
                    return;
                }
                listShow.removeAll(listSel);
                AppSpUtils.saveCollectList(listShow);
                ToastUtils.showShort(getString(R.string.delete_text));
                cancelEdit();
                getData();
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        if (isEdit) {
            if (collectAdapter.getItem(position).isSelected()) {
                collectAdapter.getItem(position).setSelected(false);
                listSel.remove(collectAdapter.getItem(position));
            } else {
                collectAdapter.getItem(position).setSelected(true);
                listSel.add(collectAdapter.getItem(position));
            }
            collectAdapter.notifyItemChanged(position);
        }
    }

    @Override
    protected void initData() {
        getData();
    }

    private void getData() {
        listShow.clear();
        List<StkResBean> listSp = AppSpUtils.getCollectList();
        if (listSp != null && listSp.size() > 0) {
            listShow.addAll(listSp);
            collectAdapter.setList(listShow);
            mDataBinding.rvCollectList.setVisibility(View.VISIBLE);
            mDataBinding.tvCollectNoData.setVisibility(View.GONE);
        } else {
            mDataBinding.rvCollectList.setVisibility(View.GONE);
            mDataBinding.tvCollectNoData.setVisibility(View.VISIBLE);
        }
    }

    private void cancelEdit() {
        isEdit = false;
        listSel.clear();
        collectAdapter.setEdit(isEdit);
        getData();
        mDataBinding.llDelete.setVisibility(View.GONE);
    }

}